package com.burgeon.backend.SHOPBACKEND.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.burgeon.backend.SHOP.entity.Order;
import com.burgeon.backend.SHOP.mappers.ShopTempateMapper;
import com.burgeon.backend.SHOP.service.ShopMessageTemplateService;
import com.burgeon.backend.SHOPBACKEND.entity.TemplateSwtich;
import com.burgeon.backend.SHOPBACKEND.entity.TemplateUser;
import com.burgeon.backend.SHOPBACKEND.mappers.OrderRateMapper;
import com.burgeon.backend.SHOPBACKEND.service.OrderRateService;
import com.burgeon.backend.SHOPBACKEND.service.ex.DataNotFoundException;
import com.burgeon.backend.SHOPBACKEND.service.ex.UpDataException;

import com.burgeon.backend.service.QueueService;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

@Service
@Log
public class OrderRateServiceImpl implements OrderRateService {
    @Autowired
    private OrderRateMapper orderRateMapper;
    @Autowired
    private ShopMessageTemplateService shopMessageTemplateService;
    @Autowired
    private QueueService queueService;

    @Override
    public void OrderRateTemplate() {
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        sdf.format(date);
        Calendar calendar1 = Calendar.getInstance();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        //线上订单未评价X天之后发送消息
        //评价开启的客户
        ArrayList<TemplateSwtich> tss = orderRateMapper.findTemplateSwitch6();
        for (TemplateSwtich ts:tss) {
            calendar1.add(Calendar.DATE, -ts.getTemplateSwitch6Value()+1);
            String rateDate = sdf1.format(calendar1.getTime());
            //获取这些客户的状态为3的订单
            log.info("过去的时间"+rateDate);
            ArrayList<Order> orders = orderRateMapper.findOrderByState(ts.getToken(),rateDate);
//            System.err.println(orders);
            for (Order o:orders) {
//                System.err.println(o.getModifiedTime());
                String goodsName = orderRateMapper.findGoodsByOid(o.getId());
                TemplateUser user = orderRateMapper.findUserByPhone(o.getCreatedUser(),ts.getToken());
                //发送模板消息
                if(!"0".equals(o.getTotalPrice())){
                    String price=o.getTotalPrice()+"元";
                    queueService.putOrderRate(user.getOpenid(),o.getOrderId(),ts.getToken(),o.getTotalPrice(),goodsName);
//                    shopMessageTemplateService.sendOrderRate(user.getOpenid(),o.getOrderId(),ts.getToken(),o.getTotalPrice(),goodsName);
                }else {
                    String price=o.getTotalIntegral()+"积分";
//                    shopMessageTemplateService.sendOrderRate(user.getOpenid(),o.getOrderId(),ts.getToken(),o.getTotalIntegral(),goodsName);
                    queueService.putOrderRate(user.getOpenid(),o.getOrderId(),ts.getToken(),o.getTotalIntegral(),goodsName);
                }

            }


        }

    }

    /**
     * 查询是否发送评价
     * @param account
     * @return
     */
    @Override
    public TemplateSwtich getTemplateSwitch6(String account) {
        if(null==account || account.isEmpty()){
            throw new DataNotFoundException("请确定查询的用户");
        }
        TemplateSwtich templateSwtich =orderRateMapper.getTemplateSwitch6(account);
        return templateSwtich;
    }

    /**
     * 修改是否启用
     * @param account
     * @param isUse
     * @return
     */
    @Override
    public Integer upIsUse(String account,String isUse,String templateSwitch6Value) {
        Integer data = orderRateMapper.upIsUse(account,isUse,templateSwitch6Value);
        if(0==data){
            throw new UpDataException("修改失败");
        }
        return data;
    }


}
